package com.yunji.coupon.dao.impl;

import com.yunji.common.base.BaseDaoImpl;
import com.yunji.coupon.dao.ICouponOrderDao;
import com.yunji.coupon.doamin.CouponOrderDO;
import com.yunji.coupon.mapper.ICouponOrderMapper;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * @author : peter-zhu
 * @date : 2024/10/28 20:23
 * @description : TODO
 **/
@Repository
public class CouponOrderDaoImpl extends BaseDaoImpl<ICouponOrderMapper, CouponOrderDO> implements ICouponOrderDao {

    @Override
    public boolean removeOrderCoupon(Long orderId) {
        return lambdaUpdate().eq(CouponOrderDO::getOrderId, orderId).remove();
    }

    @Override
    public Long getCidByOid(Long orderId) {
        return Optional.ofNullable(lambdaQuery().select(CouponOrderDO::getCouponId).eq(CouponOrderDO::getOrderId, orderId).one()).map(CouponOrderDO::getCouponId).orElse(null);
    }

    @Override
    public boolean existsByCid(Long couponId) {
        return lambdaQuery().eq(CouponOrderDO::getCouponId, couponId).exists();
    }
}
